//
//  SettingView.m
//  Pay
//
//  Created by steve on 15-1-14.
//  Copyright (c) 2015年 steve. All rights reserved.
//

#define GREEN_SETTING_EACH @"GREEN_SETTING_EACH"
#define GREEN_SETTING_PER @"GREEN_SETTING_PER"

#import "SettingView.h"

@interface SettingView ()

@property (weak, nonatomic) IBOutlet UITextField *eachText;
@property (weak, nonatomic) IBOutlet UITextField *perText;

@end

@implementation SettingView

#pragma mark - private -

- (void)saveEach:(NSNumber *)each
             per:(NSNumber *)per {
  NSAssert(each, @"each 不可空");
  NSAssert(per, @"per 不可空");
  [[NSUserDefaults standardUserDefaults] setObject:each
                                            forKey:GREEN_SETTING_EACH];
  [[NSUserDefaults standardUserDefaults] setObject:per
                                            forKey:GREEN_SETTING_PER];
  [[NSUserDefaults standardUserDefaults] synchronize];
}

#pragma mark - action -

- (IBAction)onCancel:(id)sender {
    [self removeFromSuperview];
}

- (IBAction)onSave:(id)sender {
  if ([self.eachText.text length] == 0) {
    [[[UIAlertView alloc] initWithTitle:nil
                               message:@"Must have numbers."
                              delegate:nil
                     cancelButtonTitle:@"Ok"
                      otherButtonTitles:nil] show];
    return;
  }
  
  if ([self.perText.text length] == 0) {
    [[[UIAlertView alloc] initWithTitle:nil
                                message:@"Must have numbers."
                               delegate:nil
                      cancelButtonTitle:@"Ok"
                      otherButtonTitles:nil] show];
    return;
  }
  
  [self saveEach:[NSNumber numberWithInt:[self.eachText.text intValue]]
             per:[NSNumber numberWithInt:[self.perText.text intValue]]];
  
  [self removeFromSuperview];
}

@end
